import pygame
from pygame.locals import *
from State import *
from Field import *
from Menu import *
from Dice import *
from drawing import *
from Gui import Gui, Window, Button

class Arena(State):
   def __init__(self, screen):
      State.__init__(self, 'Arena')
      self.screen = screen
      self.menu = Menu()
      self.field = Field(self.menu.get_mode)
      self.dice = Dice()
      self.field_image = pygame.image.load('gfx/field.png')
      self.player_image = pygame.image.load('gfx/player.png')
      self.player_image.set_colorkey((255,0,255))
      self.gui = Gui()
      font = pygame.font.get_default_font()
      window = Window((10, 400, 290, 61), '', font, drawtop=False)
      window.add(Button((10, 10, 60, 18), "Move", font, 12))
      window.add(Button((80, 10, 60, 18), "Tackle", font, 12))
      window.add(Button((150, 10, 60, 18), "Foul", font, 12))
      window.add(Button((220, 33, 60, 18), "Go For It", font, 12))
      
      window.add(Button((10, 33, 60, 18), "Pass", font, 12))
      window.add(Button((80, 33, 60, 18), "Handoff", font, 12))
      window.add(Button((150, 33, 60, 18), "Pickup", font, 12))
      self.gui.add(window)
   def update(self):
      #print self.mode, self.selected
      mpos = pygame.mouse.get_pos()
      if self.gui.getClick(mpos):
         self.gui.update()
      else:
         for event in pygame.event.get():
            if event.type == pygame.QUIT: self.run = False
            elif event.type == MOUSEBUTTONUP:
               mpos = event.pos
               mapr = self.field.rect
               menur = self.menu.rect
               dicer = self.dice.rect
               if mapr.collidepoint(mpos):
                  print "click on map"
                  self.field.click(mpos)
               elif menur.collidepoint(mpos):
                  print "click on menu"
                  self.menu.click(mpos)
               elif dicer.collidepoint(mpos):
                  print "click on dice"
                  self.dice.click(mpos)
               else:
                  print "no target for click"
            elif event.type == KEYDOWN:
               if event.key == K_ESCAPE:
                  self.run = False
               if event.key == K_q:
                  self.run = False

   def draw(self):
      self.screen.fill((0,0,0))
      draw_field(self.screen, self.field.rect, self.field_image)
      if self.field.selected:
         draw_selected(self.screen, self.field.selected, self.field.rect)
      draw_players(self.screen, self.field.teams, self.field.rect, self.player_image)
      self.gui.draw()
      pygame.display.flip()
